home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1992 by Michael J. Roberts. All Rights Reserved. */
- /*
- Name
- uniqind.c - clean up generated index by combining repeated entries
- Function
- Takes a sorted index file (generated by a TeX file such as tads5.tex),
- and combines repeated index lines into single index entries (with
- comma-separated page lists). Also groups runs of each initial letter
- by inserting \medskip between each letter run.
-
- Reads standard input, writes standard output.
- Notes
- Some hand-editing is probably still required after this pass!
- Modified
- 09/07/92 MJRoberts - creation
- */
-
- #include <stdio.h>
- #include <string.h>
-
- main()
- {
- char buf[128];
- char prv[128];
- char key[50];
- char out[128];
- char *outp = out;
- char *p;
- char letter;
- char newlet;
- int pagelist[50];
- int pagecnt = 0;
- int i;
- int j;
- int cur;
-
- for (;;)
- {
- if (!gets(buf)) break;
-
- p = strstr(buf, " !0 ");
- if (!p)
- {
- fprintf(stderr, "bad line: \"%s\"\n", buf);
- }
- else
- {
- /* get the current key */
- memcpy(key, buf, (size_t)(p - buf));
- key[p - buf] = '\0';
-
- /* if there's no old key, or this key differs, start new line */
- if (outp == out
- || (!(strlen(prv) == p - buf - 1
- && isalpha(key[0])
- && key[p - buf - 1] == 's'
- && !memicmp(prv, key, (size_t)(p - buf - 1)))
- && stricmp(key, prv)))
- {
- /* if there's a line pending, write it */
- if (outp != out)
- {
- newlet = (isupper(out[0]) ? out[0] : toupper(out[0]));
- if (isalpha(out[0]) && newlet != letter)
- puts("\n\\medskip\n");
-
- for (i = 0 ; i < pagecnt ; ++i)
- {
- if (i)
- {
- *outp++ = ',';
- *outp++ = ' ';
- }
- sprintf(outp, "%d", pagelist[i]);
- outp += strlen(outp);
- }
- *outp++ = '.';
- *outp = '\0';
- puts(out);
- outp = out;
- letter = (isupper(out[0]) ? out[0] : toupper(out[0]));
- }
-
- /* save the new key, and start building the line */
- strcpy(prv, key);
-
- if (!isalpha(key[0]))
- sprintf(out, "{\\tt %s}, ", key);
- else
- sprintf(out, "%s, ", key);
-
- /* make first entry in page list */
- pagecnt = 1;
- pagelist[0] = atoi(p + 4);
- outp += strlen(out);
- }
- else
- {
- /* it's a match - simply add new page number */
- cur = atoi(p + 4);
- for (i = 0 ; i < pagecnt ; ++i)
- {
- if (pagelist[i] == cur) goto do_not_add;
- if (pagelist[i] > cur) break;
- }
- for (j = i ; j < pagecnt ; ++j)
- pagelist[j+1] = pagelist[j];
- pagelist[i] = cur;
- ++pagecnt;
- do_not_add: ;
- }
- }
- }
-
- /* if there's a line pending, write it */
- if (outp != out)
- {
- newlet = (isupper(out[0]) ? out[0] : toupper(out[0]));
- if (isalpha(out[0]) && newlet != letter)
- puts("\n\\medskip\n");
-
- for (i = 0 ; i < pagecnt ; ++i)
- {
- if (i)
- {
- *outp++ = ',';
- *outp++ = ' ';
- }
- sprintf(outp, "%d", pagelist[i]);
- outp += strlen(outp);
- }
- *outp++ = '.';
- *outp = '\0';
-
- puts(out);
- outp = out;
- }
- }
-